package com.scau.yangsuiyu.estore.controller.buyer;

import com.scau.yangsuiyu.estore.controller.myComponent.CartGoodsHBox;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.Item;
import com.scau.yangsuiyu.estore.dababases.entity.Orders;
import com.scau.yangsuiyu.estore.service.CartService;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Pagination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 购物车界面
 * @date 2019-06-11 11:28
 * @email suiyu_yang@163.com
 */
public class CartPane extends AnchorPane implements Initializable {
    private Buyer buyer;
    private int itemsPerPage = 5;
    private VBox itemsVBox = new VBox();
    private List<Item> itemsOfCartList;

    private List<Item> selectedItemList = new ArrayList<>();
    private List<CartGoodsHBox> itemHBoxList = new ArrayList<>();

    private double totalPrice = 0;
    private double paymentPrice = 0;
    private int amount = 0;

    @FXML
    private VBox mainVBox;

    @FXML
    private CheckBox selectedAllCB;

    @FXML
    private Pagination pagination;

    public CartPane(Buyer buyer) {
        this.buyer = buyer;
        this.itemsOfCartList = new CartService().getAllItemOfCart(buyer);

        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/buyer/CartPane.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        updatePages();

        mainVBox.getChildren().add(itemsVBox);
    }

    @FXML
    private void selectAllClick() {
        for (CartGoodsHBox itemHBox : itemHBoxList) {
            itemHBox.setSelectGoods(selectedAllCB.isSelected());
        }
    }

    @FXML
    private void buyClick() {
        updateSelectItemFromUI();
        new ConfirmOrderPane(buyer, selectedItemList).showPane();
    }

    private void updateSelectItemFromUI() {
        selectedItemList.clear();
        for (CartGoodsHBox itemHBox : itemHBoxList) {
            if (itemHBox.isItemSelected()) {
                selectedItemList.add(itemHBox.getItem());
            }
        }
    }

    /**
     * 更新页面
     */
    public void updatePages() {
        itemsOfCartList = new CartService().getAllItemOfCart(buyer);
        itemsVBox.getChildren().clear();
        itemHBoxList.clear();

        int pageNum;
        if (itemsOfCartList.size() % itemsPerPage == 0) {
            pageNum = itemsOfCartList.size() / itemsPerPage;
        } else {
            pageNum = itemsOfCartList.size() / itemsPerPage + 1;
        }
        pagination.setPageCount(pageNum);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
    }

    /**
     * 创建每一页的对象
     *
     * @param pageIndex
     * @return
     */
    private VBox createPage(int pageIndex) {
        itemHBoxList.clear();
        int page = pageIndex * itemsPerPage; //当前是第几个商品
        if (itemsOfCartList.size() != 0) {
            itemsVBox.getChildren().clear();
            for (int i = page; i < page + itemsPerPage; i++) {
                if (i < itemsOfCartList.size()) {
                    Item temp = itemsOfCartList.get(i);
                    CartGoodsHBox cartGoodsHBox = new CartGoodsHBox(this, temp);
                    itemHBoxList.add(cartGoodsHBox);
                }
            }
        }
        itemsVBox.getChildren().addAll(itemHBoxList);
        return itemsVBox;
    }
}
